package com.zyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyg.dao.UserMapper;
import com.zyg.pojo.EchartsData;
import com.zyg.pojo.Permission;
import com.zyg.pojo.User;
import com.zyg.pojo.UserRole;
import com.zyg.service.PermissionService;
import com.zyg.service.UserRoleService;
import com.zyg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private PermissionService permissionService;

    @Override
    public List<User> findAll(User user, int offset, int pageSize) {

        return userMapper.findAll(user,offset,pageSize);
    }

    @Override
    @Transactional
    public int addUser(User user) {
        //统一事务，确保数据库操作成功，否则就会回滚！
        int result = userMapper.addUser(user);
        //数据不含有主键，需要返回主键
        //获取用户主键
        int userId = user.getId();
        //往userRole表插入数据，调用save方法,save是单个查入，savebatch是批量插入
        //对数据做处理，组装为list
        Integer[] roleIds = user.getRoleIds();
        List<UserRole> userRoleList=new ArrayList<>();
        if(roleIds!=null && roleIds.length>0){
            for(Integer roleId:roleIds){
                UserRole userRole=new UserRole();
                userRole.setUId(userId);
                userRole.setRId(roleId);
                userRoleList.add(userRole);
            }
        }//组成list，把1，2，3变成1，1  1，2  1，3
        userRoleService.saveBatch(userRoleList);
        return result;
    }

    @Override
//    public int deleteUser(int id) {
//        return userMapper.deleteUser(id);
//    }
    public int deleteUser(int id) {
        // 删除用户信息
        int result = userMapper.deleteUser(id);

        // 删除用户角色关联信息
        QueryWrapper<UserRole> qw = new QueryWrapper<>();
        qw.eq("u_id", id);
        userRoleService.remove(qw);
        return result;
    }


    @Override
    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public int updateUser(User user) {
        int result=userMapper.updateUser(user);
        QueryWrapper<UserRole> qw=new QueryWrapper<>();
        qw.eq("u_id",user.getId());
        userRoleService.remove(qw);
        int userId = user.getId();
        Integer[] roleIds = user.getRoleIds();
        List<UserRole> userRoleList=new ArrayList<>();
        if(roleIds!=null && roleIds.length>0){
            for(Integer roleId:roleIds){
                UserRole userRole=new UserRole();
                userRole.setUId(userId);
                userRole.setRId(roleId);
                userRoleList.add(userRole);
            }
        }//组成list，把1，2，3变成1，1  1，2  1，3
        userRoleService.saveBatch(userRoleList);
        return result;
    }

    @Override
    public int count(User user) {
        return userMapper.count(user);
    }

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    public User findUserByUserName(String username) {
        User user = userMapper.findUserByUserName(username);
        return user;
    }
    @Override
    public List<EchartsData> getDeptIndexData() {
        return userMapper.getDeptIndexData();
    }

    @Override
    public List<Permission> getMenuList() {
        List<Permission> menuList = permissionService.getMenuList();
        return menuList;
    }

    @Override
    public List<Permission> getMenuListByUserId(Integer loginId) {
        List<Permission> menuList = permissionService.getMenuListByUserId(loginId);
        return menuList;
    }
}
